package org.iweb.ioc;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.iweb.init.AbstractRegister;
import org.iweb.init.Register;
import org.iweb.init.scanner.AnnotationScanner;

@SuppressWarnings({ "rawtypes", "unchecked" })
@Register(name="IocRegister")
public class IocRegister extends AbstractRegister{

	private Map<String,InjectObj> instances;
	
	
	@Override
	public void populate(AnnotationScanner scanner) {
		List<Class> annotatedClasses= scanner.getListByAnnotation(Injectable.class);		
		for(Class c: annotatedClasses){				
			Injectable ann=(Injectable) c.getAnnotation(Injectable.class);			
			getInstances().put(ann.value(),new InjectObj(c,null,ann.singleton(),ann.init_method(),ann.lazy()));								
		}
		
		InjectObj bobj=null;
		
		for(String id: getInstances().keySet()){			
			bobj=getInstances().get(id);			
			if(!bobj.isLazy()){				
				ServiceLookup.getInstance().get(id);				
			}			
		}		
		
	}
	
	public Map<String,InjectObj> getInstances() {
		if(instances==null)
			instances=new HashMap<String,InjectObj>();
		return instances;
	}

	@Override
	public void clear() {
		getInstances().clear();		
	}

	@Override
	public Long getNumReg() {
		return new Long(getInstances().size());
	}

}
